package org.jhotdraw.draw;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

aspect Logging {
	private ArrayList<String> log = new ArrayList<String>();

	private void gravarLog() throws IOException {
		DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HHmmss");
		Calendar cal = Calendar.getInstance();
		String data = dateFormat.format(cal.getTime());
		File newFile = new File("C:\\log jhotdraw " + data + ".txt");
		FileWriter fstream = new FileWriter(newFile);
		BufferedWriter out = new BufferedWriter(fstream);
		for (String s : log) {
			out.write(s + "\r\n");
		}

		out.close();
		fstream.close();

	}

	pointcut figuraAdicionada(Figure f):
		target(f) && call (void AbstractFigure.fireFigureAdded());

	after(Figure f): figuraAdicionada(f){
		log.add(f.toString().split("@")[0] + " foi adicionado");
	}

	pointcut figuraRemovida(Figure f):
		target(f) && call (void AbstractFigure.fireFigureRemoved());

	after(Figure f): figuraRemovida(f){
		log.add(f.toString().split("@")[0] + " foi removido");
	}

	pointcut gerarLog():
		call (void org.jhotdraw.app.Disposable.dispose());

	before(): gerarLog(){
		try {
			gravarLog();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
